package com.yangluyao.easyexcel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.yangluyao.easyexcel.model.EasyExcelModel;
import com.yangluyao.easyexcel.model.QualityUser;
import com.yangluyao.easyexcel.read.listener.EasyExcelReadListener;
import com.yangluyao.easyexcel.read.listener.QualityUserListener;
import com.yangluyao.easyexcel.service.IEasyExcelService;
import com.yangluyao.easyexcel.service.IQualityUserService;
import com.yangluyao.easyexcel.utils.ApiResponse;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.CacheResponse;
import java.net.URLEncoder;
import java.util.ArrayList;

/*
* @Description: TODO
* @Author:  hejiayao
* @CreateDate:  2021/7/2 16:14
* @UpdateUser:  hejiayao
* @UpdateDate:  2021/7/2 16:14
* @className: QualityUserController
* @Package : com.yangluyao.easyexcel.controller
* @Version: 1.0
*/
@RestController
public class QualityUserController {


    @Autowired
    private IQualityUserService qualityUserService;
    /**
     * @Author hejiayao
     * @Description //TODO  数据上传模板的生成
     * @method tempalte
     * @Date 16:16 2021/7/2
     * @param response
     * @return void
     **/
    @GetMapping("/QualityUserTemplate")
    public void tempalte(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("QualityUser数据上传模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //需要合并的列
        int[] mergeColumeIndex = {1, 3};
        //从第二行后开始合并
        int mergeRowIndex = 2;
        // 调用合并单元格工具类，此工具类是根据工程名称相同则合并后面数据
        ExcelMergeUtil excelFillCellMergeStrategy = new ExcelMergeUtil(mergeRowIndex,mergeColumeIndex);
        // 这里需要设置不关闭流
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)13);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), QualityUser.class ).registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(excelFillCellMergeStrategy).sheet("QualityUser数据上传模板").doWrite(new ArrayList<QualityUser>());
    }



    /**
     * @Author hejiayao
     * @Description //TODO 数据导出
     * @method download
     * @Date 14:49 2021/7/2
     * @param response
     * @return void
     **/
    @GetMapping("/qualityExport")
    public void qualityExport(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("qualityExport数据表格", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), QualityUser.class).sheet("数据表格").doWrite(qualityUserService.list());
    }


    /**
     * @Author hejiayao
     * @Description //TODO 导入
     * @method upload
     * @Date 14:49 2021/7/2
     * @param file
     * @return java.lang.String
     **/
    @PostMapping("/qualityImport")
    @ResponseBody
    public ResponseEntity<ApiResponse> qualityImport(MultipartFile file) throws IOException {

        ApiResponse apiResponse=new ApiResponse();

        EasyExcel.read(file.getInputStream(),QualityUser.class , new QualityUserListener(qualityUserService,apiResponse)).sheet().doRead();
        return ResponseEntity.ok(ApiResponse.success(apiResponse));
    }
}
